package cn.edu.zafu.bigdata.llc;

import java.io.*;
import java.net.*;

public class TCPClient {
    public static void main(String[] args) {
        String hostname = "localhost"; // 服务器地址，如果是连接到同学的服务器，替换为同学的IP地址
        int port = 1234; // 服务器端口号

        try (Socket socket = new Socket(hostname, port)) {
            System.out.println("Connected to server.");

            // 创建输入输出流
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));

            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                // 发送消息到服务器
                out.println(userInput);

                // 读取服务器回复的消息
                String fromServer = in.readLine();
                System.out.println("Server says: " + fromServer);
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostname);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " + hostname);
            System.exit(1);
        }
    }
}

